package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.SignConfig;
import com.wangsd.web.coupon.entity.SignConfigDetail;
import com.wangsd.web.coupon.mapper.SignConfigDetailMapper;
import com.wangsd.web.coupon.mapper.SignConfigMapper;
import com.wangsd.web.coupon.service.SignConfigDetailService;
import com.wangsd.web.coupon.service.SignConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-15
 */
@Service
public class SignConfigServiceImpl extends ServiceImpl<SignConfigMapper, SignConfig> implements SignConfigService {

    @Resource
    SignConfigMapper signConfigMapper;

    @Resource
    SignConfigDetailMapper signConfigDetailMapper;

    @Autowired
    SignConfigDetailService signConfigDetailService;

    @Override
    public IPage<SignConfig> selectPage(Integer pageNum, Integer pageSize, SignConfig config) {
        Page<SignConfig> page = new Page<>(pageNum, pageSize);
        IPage<SignConfig> resultIPage = signConfigMapper.selectListXml(page, config);
        return resultIPage;
    }

    @Override
    public List<SignConfigDetail> selectDetailBySignConfigId(Long id) {
        QueryWrapper<SignConfigDetail> query = new QueryWrapper<>();
        query.eq("sign_config_id", id);
        List<SignConfigDetail> list = signConfigDetailMapper.selectList(query);
        return list;
    }

    @Override
    public List<SignConfigDetail> selectDetailByCompanyId(Long companyId) {
        List<SignConfigDetail> signConfigDetails = signConfigDetailMapper.selectDetailByCompanyId(companyId);
        return signConfigDetails;
    }

    @Override
    public int updateStatus(SignConfig config) {
        /*如果当前配置要上架，那么就需要将商圈中的其他配置置为下架状态*/
        if (config.getStatus() == 1) {
            QueryWrapper<SignConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("company_id",config.getCompanyId());
            SignConfig newSignConfig = new SignConfig();
            newSignConfig.setStatus(0);
            signConfigMapper.update(newSignConfig,queryWrapper);
        }
        signConfigMapper.updateById(config);
        return 1;
    }

    @Override
    public SignConfig selectByCompanyId(Long id) {
        QueryWrapper<SignConfig> query = new QueryWrapper<>();
        query.eq("company_id",id);
        query.eq("status",1);
        SignConfig signConfig = signConfigMapper.selectOne(query);
        return signConfig;
    }

    @Override
    public void addOrUpdate(SignConfig entity) {
        /*新增*/
        if (entity.getId() == null) {
            signConfigMapper.insert(entity);
            /*保存明细配置*/
            this.saveConfigDetail(entity.getId(), entity.getSignConfigDetailList());
        } else {
            /*修改*/
            /*先删除之前的管理关系*/
            this.deleteConfigDetail(entity.getId());
            /*更新数据*/
            signConfigMapper.updateById(entity);
            /*保存明细配置*/
            this.saveConfigDetail(entity.getId(), entity.getSignConfigDetailList());
        }
    }

    private void saveConfigDetail(Long id, List<SignConfigDetail> list) {
        if (list != null && list.size() > 0) {
            List<SignConfigDetail> signConfigDetails = new ArrayList<>();
            list.forEach(signConfigDetail -> {
                SignConfigDetail configDetail = new SignConfigDetail();
                configDetail.setCreatedTime(new Date());
                configDetail.setUpdatedTime(new Date());
                configDetail.setDay(signConfigDetail.getDay());
                configDetail.setPoints(signConfigDetail.getPoints());
                configDetail.setSign_config_id(id);
                signConfigDetails.add(configDetail);
            });
            signConfigDetailService.saveBatch(signConfigDetails);
        }
    }

    private void deleteConfigDetail(Long sign_config_id) {
        QueryWrapper<SignConfigDetail> query =new QueryWrapper<>();
        query.eq("sign_config_id", sign_config_id);
        signConfigDetailMapper.delete(query);
    }
}
